﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using MediaRecommender.Security;

namespace MediaRecommender.Model
{
    //User type
    [Serializable()]
    public class User : IEquatable<User>, ISerializable
    {
        #region Fields
        private string _name;
        private string password;
        private DateTime _birthday;
        private Sex _sex;
        private MediaList _mediaList;
        private List<int> _preferences;
        private List<User> _friends;
        private List<Media> _recommendations;
        #endregion

        #region Propterties
        public string Name 
        {
            get { return this._name; }
            set { this._name = value; }
        }

        public string Password
        {
            get { return this.password; }
            private set { this.password = value; }
        }

        public int Age
        {
            get 
            {
                if (DateTime.Now.Month <= this.Birthday.Month)
                {
                    return DateTime.Now.Year - this.Birthday.Year - 1;
                }
                else if (DateTime.Now.Month == this.Birthday.Month && DateTime.Now.Day < this.Birthday.Day)
                {
                    return DateTime.Now.Year - this.Birthday.Year - 1;
                }
                else
                {
                    return DateTime.Now.Year - this.Birthday.Year;
                }
            }
        }

        public Sex Sex
        {
            get { return this._sex; }
            private set { this._sex = value; }
        }

        public DateTime Birthday
        {
            get { return this._birthday; }
            private set { this._birthday = value; }
        }

        public MediaList MediaList
        {
            get { return this._mediaList; }
            private set { this._mediaList = value; }
        }

        public List<int> Preferences
        {
            get { return this._preferences; }
            set { this._preferences = value; } 
        }

        public List<User> Friends
        {
            get { return this._friends; }
            private set { this._friends = value; }
        }

        public List<Media> Recommendations
        {
            get { return this._recommendations; }
            private set { _recommendations = value; }
        }
        #endregion

        #region Constructor
        public User(string name, string password, DateTime birthday, Sex sex, List<int> preferences)
        {
            this.Name = name;
            this.Password = PasswordHash.CreateHash(password);
            this.Birthday = birthday;
            this.Sex = sex;
            this.Preferences = preferences;
            this._friends = new List<User>();
            this._mediaList = new MediaList(this);
            this._recommendations = new List<Media>();
        }
        #endregion

        /*
         * Serialization uses a method. the defines what fields you want saved and what their names are
         * to deserialize it puts that data through a constructor where the matches the data with the names and creates the object
         * Therefore its essential that the variable names match
         */
        #region Serialization
        public User(SerializationInfo info, StreamingContext ctxt)
        {
            this.Name = (String)info.GetValue("Name", typeof(String));
            this.Password = (String)info.GetValue("Password", typeof(String));
            this.Birthday = (DateTime)info.GetValue("Birthday", typeof(DateTime));
            this.Sex = (Sex)info.GetValue("Sex", typeof(Sex));
            this.MediaList = (MediaList)info.GetValue("MediaList", typeof(MediaList));
            this.Preferences = (List<int>)info.GetValue("Preferences", typeof(List<int>));
            this.Friends = (List<User>)info.GetValue("Friends", typeof(List<User>));
            this.Recommendations = (List<Media>)info.GetValue("Recommendations", typeof(List<Media>));
        }
 
        public virtual void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Name", this.Name);
            info.AddValue("Password", this.Password);
            info.AddValue("Birthday", this.Birthday);
            info.AddValue("Sex", this.Sex);
            info.AddValue("MediaList", this.MediaList);
            info.AddValue("Preferences", this.Preferences);
            info.AddValue("Friends", this.Friends);
            info.AddValue("Recommendations", this.Recommendations);
        }
        #endregion

        #region Equals
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (this.GetType() != obj.GetType())
                return false;

            return Equals((User)obj);
        }

        public bool Equals(User obj)
        {
            if (obj == null)
                return false;
            if (ReferenceEquals(this, obj))
                return true;

            if (GetHashCode() != obj.GetHashCode())
                return false;

            return this.Name.Equals(obj.Name) &&
                this.Age.Equals(obj.Age) &&
                this.Sex.Equals(obj.Sex) &&
                this.Birthday.Equals(obj.Birthday);
        }

        public static bool operator ==(User leftUser, User rightUser)
        {
            if (ReferenceEquals(leftUser, null))
                return ReferenceEquals(rightUser, null);

            return (leftUser.Equals(rightUser));
        }

        public static bool operator !=(User leftUser, User rightUser)
        {
            return !(leftUser == rightUser);
        }
        #endregion
    }
}